package com.spvs.job.task;


import cn.hutool.core.date.DateUtil;
import com.spvs.common.core.constant.NoticeConstant;
import com.spvs.common.core.utils.DateUtils;
import com.spvs.job.domain.*;
import com.spvs.job.mapper.EquipUpkeepPlanRecordMapper;
import com.spvs.job.mapper.EquipUpkeepRecordDetailMapper;
import com.spvs.job.mapper.EquipUpkeepRecordMapper;
import com.spvs.system.api.IEquipNoticeService;
import com.spvs.system.api.domain.EquipNotice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 保养任务
 *
 * @author supervision
 */
@Component
@Slf4j
public class UpkeepRecord {

    @Autowired
    private EquipUpkeepPlanRecordMapper upkeepPlanRecordMapper;

    @Autowired
    private EquipUpkeepRecordMapper upkeepRecordMapper;
    @Autowired
    private EquipUpkeepRecordDetailMapper upkeepRecordDetailMapper;

    @Autowired
    private IEquipNoticeService equipNoticeService;




    public void executeTasks() {
        // log.info("保养计划任务。。。。");
        List<EquipUpkeepPlanRecord> equipUpkeepPlanRecords = upkeepPlanRecordMapper.
                selectAll(CommonEnum.YOU_XIAO.getCode());
        if (CollectionUtils.isEmpty(equipUpkeepPlanRecords)) {
            // log.info("未查询到保养计划。。。。");
            return;
        }
        log.info("查询到保养计划任务{}条。。。。", equipUpkeepPlanRecords.size());
        for (EquipUpkeepPlanRecord equipUpkeepPlanRecord : equipUpkeepPlanRecords) {
            Long planRecordId = equipUpkeepPlanRecord.getId();
            UpkeepEnum byFormCode = UpkeepEnum.getByFormCode(equipUpkeepPlanRecord.getCycle());
            List<EquipUpkeepRecordDetail> equipUpkeepRecordDetails = upkeepRecordDetailMapper.selectEquipUpkeepRecordDetailByPlanId(planRecordId);
            if (CollectionUtils.isEmpty(equipUpkeepRecordDetails)) {
                log.info("{}未查询到绑定设备详情信息。。。", equipUpkeepPlanRecord.getName());
                continue;
            }
            labe:
            for (EquipUpkeepRecordDetail recordDetail : equipUpkeepRecordDetails) {
                EquipUpkeepRecord equipUpkeepRecord =
                        upkeepRecordMapper.selectEquipUpkeepRecordByCode(recordDetail.getDeviceId(), 0);
                if (equipUpkeepRecord != null) {
                    String days = DateUtils.dayDiff(equipUpkeepRecord.getPlanUpkeepTime(), new Date());
                    if (Integer.parseInt(days) < Integer.parseInt(byFormCode.getCode())) continue labe;
                    extracted(planRecordId, recordDetail);
                } else {
                    extracted(planRecordId, recordDetail);
                }
            }
        }
    }

    private void extracted(Long planRecordId, EquipUpkeepRecordDetail recordDetail) {
        EquipUpkeepRecord upkeepRecord = new EquipUpkeepRecord();
        upkeepRecord.setPlanId(planRecordId);
        upkeepRecord.setDeviceId(recordDetail.getDeviceId());
        upkeepRecord.setEquipmentCode(recordDetail.getEquipmentCode());
        upkeepRecord.setEquipmentName(recordDetail.getEquipmentName());
        upkeepRecord.setPlanUpkeepTime(DateUtil.beginOfDay(new Date()));
        upkeepRecord.setLiablePerson(recordDetail.getLiablePerson());
        upkeepRecord.setDeptId(recordDetail.getDeptId());
        upkeepRecord.setState(0);
        upkeepRecord.setType(0);
        upkeepRecordMapper.insertEquipUpkeepRecord(upkeepRecord);
        EquipNotice equipNotice = new EquipNotice();
        equipNotice.setContent("您有一条保养任务有待执行，请及时查看");
        equipNotice.setTitle(NoticeConstant.TITLE_1);
        equipNotice.setStatus(0L);
        equipNotice.setDeptId(recordDetail.getDeptId());
        equipNotice.setType(0L);
        equipNotice.setRemark(upkeepRecord.getId().toString());
        if (recordDetail.getLiablePerson() != null) {
            equipNotice.setType(1L);
            equipNotice.setUserId(Long.valueOf(recordDetail.getLiablePerson()));
        }
        equipNoticeService.insertEquipNotice(equipNotice);
    }
}


